/**
 * 
 */
package cn.boysoft.core.server.netty;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.socket.ServerSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

import cn.boysoft.core.server.Server;

/**
 * @author 付厚俊
 * CreateTime 2013-4-4 下午5:22:47
 */
public class NettyServer implements Server {
	ChannelPipelineFactory channelPipelineFactory;
	private int port = 9090;
	private String serverName = "";
	ServerSocketChannelFactory serverSocketChannelFactory =  new NioServerSocketChannelFactory(
            Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool());
	@Override
	public void start() throws Throwable {
		try{
		    ServerBootstrap bootstrap = new ServerBootstrap(serverSocketChannelFactory);
	        bootstrap.setPipelineFactory(channelPipelineFactory);
	        bootstrap.bind(new InetSocketAddress(port));
	        System.out.println(serverName+" NettyServer  completed.  port:"+port); 
		}catch(Throwable e){
			System.out.println(serverName+ "NettyServer start error.  port:"+port); 
			throw e;
		}
	}

	@Override
	public void stop() {
		serverSocketChannelFactory.releaseExternalResources();

	}

	public void setServerName(String serverName) {
		this.serverName = serverName;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public void setChannelPipelineFactory(
			ChannelPipelineFactory channelPipelineFactory) {
		this.channelPipelineFactory = channelPipelineFactory;
	}

}
